<script setup>
import {
  userLoginService,
  userRegisterService,
  userForgetPassword
} from '@/api/user.js'
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
// 绑定数据
const isRegister = ref(false)
const isLogin = ref(true)
const isPassword = ref(false)

// 关于登入和忘记密码、注册的表单渲染
const showRegisterForm = () => {
  isLogin.value = false
  isRegister.value = true
  isPassword.value = false
  formModel.value = { ...defaultForm }
}

const showForgotPasswordForm = () => {
  isLogin.value = false
  isRegister.value = false
  isPassword.value = true
  formModel.value = { ...defaultForm }
}

const showLoginForm = () => {
  isLogin.value = true
  isRegister.value = false
  isPassword.value = true
  formModel.value = { ...defaultForm }
}
const form = ref()
// 整个的用于提交的form数据对象
const defaultForm = {
  username: '',
  password: '',
  email: ''
}
const formModel = ref({
  ...defaultForm
})
const rules = {
  username: [
    { required: true, message: '请输入学号', trigger: 'blur' },
    { pattern: /^[0-9]{16}$/, message: '学号必须为16位的数字', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是 6-15位 的非空字符',
      trigger: 'blur'
    }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    {
      type: 'email',
      message: '请输入有效的邮箱地址',
      trigger: ['blur', 'change']
    }
  ]
}

// 注册
const register = async () => {
  // 注册成功之前，先进行校验，校验成功 → 请求，校验失败 → 自动提示
  await form.value.validate()
  await userRegisterService(formModel.value)
  ElMessage.success('注册成功')
  showLoginForm()
}

// 忘记密码
const forgetpassword = async () => {
  // 更新密码前，先进行效验
  await form.value.validate()
  await userForgetPassword()
  // formModel.value = { ...defaultForm }
  ElMessage.success('重置密码成功')
  showLoginForm()
}

// 登入
const userStore = useUserStore()
const router = useRouter()
const login = async () => {
  await form.value.validate()
  // console.log(formModel.value)
  const res = await userLoginService(formModel.value)
  userStore.setToken(res.data.data.token)
  userStore.setState(res.data.data.grade)
  userStore.getUser(res.data.data.username)
  ElMessage.success('登录成功')
  router.push('/')
}
</script>

<template>
  <el-row class="login-page">
    <el-col :span="12" :offset="2" class="bg"></el-col>
    <el-col :span="6" :offset="1" class="form">
      <!-- 登录相关表单 -->

      <el-form
        :model="formModel"
        ref="form"
        size="large"
        autocomplete="off"
        v-if="isLogin"
      >
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="formModel.username"
            :prefix-icon="User"
            placeholder="请输入用户名/学号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link
              type="primary"
              :underline="false"
              @click="showForgotPasswordForm"
              >忘记密码？</el-link
            >
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="login"
            class="button"
            type="primary"
            auto-insert-space
            >登录</el-button
          >
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="showRegisterForm">
            注册 →
          </el-link>
        </el-form-item>
      </el-form>

      <!-- 注册相关表单 -->

      <el-form
        :model="formModel"
        :rules="rules"
        ref="form"
        size="large"
        autocomplete="off"
        v-else-if="isRegister"
      >
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="formModel.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input
            v-model="formModel.email"
            :prefix-icon="Lock"
            type="email"
            placeholder="请输入你的邮箱"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="register"
            class="button"
            type="primary"
            auto-insert-space
          >
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="showLoginForm">
            ← 返回
          </el-link>
        </el-form-item>
      </el-form>

      <!-- 忘记密码表单 -->

      <el-form
        :model="formModel"
        :rules="rules"
        ref="form"
        size="large"
        autocomplete="off"
        v-else-if="isPassword"
      >
        <el-form-item>
          <h1>忘记密码</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="formModel.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="formModel.repassword"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入再次密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="forgetpassword"
            class="button"
            type="primary"
            auto-insert-space
          >
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="showLoginForm">
            ← 返回
          </el-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: rgba(196, 232, 246);
  .bg {
    background: url('@/assets/bg.png') no-repeat center;
    background-size: 100%;
    border-radius: 0 20px 20px 0;
    background-color: rgba(196, 232, 246);
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;

    .title {
      margin: 0 auto;
    }
    .button {
      width: 100%;
    }
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
  .el-form {
    background-color: #fff;
    padding: 20px;
    border-radius: 10px;
  }
}
</style>
